package com.hui.platform.falseworkapi.service.business.webmagic.pipeline;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hui.platform.falseworkapi.service.business.webmagic.entity.TbWebmagicBaiduNews;
import com.hui.platform.falseworkapi.service.business.webmagic.service.TbWebmagicBaiduNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 爬虫-百度新闻-结果处理类-持久化到MySQL
 * @author hui
 */
@Component
public class BaiduNewsPipeline implements Pipeline {

    @Autowired
    private TbWebmagicBaiduNewsService tbWebmagicBaiduNewsService;

    @Override
    public void process(ResultItems resultItems, Task task) {

        List<String> aTagList1 = resultItems.get("title1");
        List<String> aTagList2 = resultItems.get("title2");
        List<String> aTagList3 = resultItems.get("title3");
        List<String> aTagList = new ArrayList<>();
        aTagList.addAll(aTagList1);
        aTagList.addAll(aTagList2);
        aTagList.addAll(aTagList3);

        TbWebmagicBaiduNews news;
        for (String aTag : aTagList){
            String title = aTag.substring(aTag.indexOf(">") + 1, aTag.lastIndexOf("<"));
            if (checkNews(title)){
                String url = aTag.substring(aTag.indexOf("=") + 2, aTag.indexOf("\"", 10));
                news = new TbWebmagicBaiduNews();
                news.setTargetUrl(resultItems.getRequest().getUrl())
                        .setTitle(title)
                        .setUrl(url)
                        .setCreated(new Date());
                tbWebmagicBaiduNewsService.save(news);
            }
        }
    }

    private Boolean checkNews(String title){
        return tbWebmagicBaiduNewsService.getOne(new LambdaQueryWrapper<TbWebmagicBaiduNews>().eq(TbWebmagicBaiduNews::getTitle, title)) == null ? true : false;
    }
}
